package com.hs.myshell;

import java.lang.reflect.Field;
import java.util.Iterator;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.internal.UIPlugin;

import com.hs.myshell.data.DataAccess;
import com.hs.myshell.data.SshSetting;
import com.hs.myshell.utils.StringUtils;
import com.hs.myshell.views.tree.TreeObject;

/**
 * 11.1.10
 * @author Administrator
 *
 */
public class EditorDialog extends TrayDialog {

	private SshSetting sshSetting;
	
	public EditorDialog(Shell shell,TreeObject sshKey) {
		super(shell);
		DataAccess instance = DataAccess.getInstance();
		instance.init();//TODO delete
		this.sshSetting = instance.get(sshKey);
	}
	
//	protected Control createContents(Composite parent) {
//        Composite container = new Composite(parent, SWT.NONE);
//        final GridLayout gridLayout = new GridLayout();
//        gridLayout.numColumns = 1;
//        container.setLayout(gridLayout);
//
//        final Text text = new Text(container, SWT.BORDER);
//        text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
//
//        return container;
//    }
	
	public void setDialogTitle(String title) {
        if (this.getShell() != null && !this.getShell().isDisposed()) {
            this.getShell().setText(title);
        }

    }

	@Override
	protected Control createDialogArea(Composite parent) {
		// TODO Auto-generated method stub
		Control control = super.createDialogArea(parent);
		setDialogTitle("shh config");
		if (control instanceof Composite) {
			ScrolledComposite sc = new ScrolledComposite((Composite) control, 512);

			GridLayout layout = new GridLayout(1, true);
			layout.marginHeight = 0;
			layout.marginWidth = 0;
			layout.verticalSpacing = 0;
			layout.horizontalSpacing = 0;

			sc.setLayout(layout);
			sc.setLayoutData(new GridData(1808));

			sc.setExpandHorizontal(true);
			sc.setExpandVertical(true);

			Composite composite = new Composite(sc, 0);
			composite.setLayout(new GridLayout());

			createDialogAreaContent(composite);

			sc.setContent(composite);
			sc.setMinSize(composite.computeSize(-1, -1));

			control = sc;
		}
		
		return control;
	}
	
	@Override
	protected void okPressed() {
		//saveOrUpdate ssh config
		DataAccess.getInstance().saveOrUpdate(this.sshSetting);
		
		super.okPressed();
	}
	
	
	
	private Text hostText;
	private Text user;
	private Text timeout;
	private Text keepalive;
	private Text port;
	private Text password;
	
	protected void createDialogAreaContent(Composite parent) {
		Composite composite = new Composite(parent, 0);
		GridLayout gridLayout = new GridLayout(2, false);
		GridData gridData = new GridData(768);
		gridData.horizontalIndent = FieldDecorationRegistry.getDefault().getMaximumDecorationWidth();

		composite.setLayout(gridLayout);
		composite.setLayoutData(gridData);
		
		if (sshSetting == null) {
			//add
			sshSetting = new SshSetting();
		}
		Field[] fields = sshSetting.getClass().getFields();
		for (Field field : fields) {
			String name = field.getName();
		}
		
		createTextField(composite, "HOST",sshSetting.getHost());            
		createTextField(composite, "USER",sshSetting.getUser());            
		createTextField(composite, "PASSWORD",sshSetting.getPassword());    
		createTextField(composite, "TIMEOUT",sshSetting.getTimeout());      
		createTextField(composite, "KEEPALIVE",sshSetting.getKeepalive());  
		createTextField(composite, "PORT",sshSetting.getPort());            

//		this.hostText = createTextField(composite, "HOST",sshSetting.getHost());
//		this.user =     createTextField(composite, "USER",sshSetting.getUser());
//		this.password = createTextField(composite, "PASSWORD",sshSetting.getPassword());
//		this.timeout =  createTextField(composite, "TIMEOUT",sshSetting.getTimeout());
//		this.keepalive= createTextField(composite, "KEEPALIVE",sshSetting.getKeepalive());
//		this.port =     createTextField(composite, "PORT",sshSetting.getPort());
//		loadSettings();
	}
	
	private Text createTextField(Composite composite, String labelTxt,String defaultValue) {
		Label ctlLabel = new Label(composite,SWT.LEFT);
		ctlLabel.setText(labelTxt + ":");
		GridData gridData = new GridData(768);
		Text text = new Text(composite,SWT.ARROW_LEFT);
		text.setData("identifier", labelTxt);
		text.setLayoutData(gridData);
		if (!StringUtils.isBlank(defaultValue)) {
			text.setText(defaultValue);
		}
//		text.addFocusListener(new FocusListener() {
//			
//			@Override
//			public void focusLost(FocusEvent e) {
//				 System.out.println("获得焦点"+e.getSource());
//				
//			}
//			
//			@Override
//			public void focusGained(FocusEvent e) {
//				Text targetText = (Text)e.widget;
//				System.out.println("获得焦点"+targetText.getData("identifier"));
//				
//			}
//		});
		text.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				Text targetText = (Text)e.widget;
				targetText.getData("identifier");
				System.out.println("modifyText： " + e.widget);
			}
		});
		return text;
	}
	

}
